import json
import requests


def do_get(url, token=None, params=None):
    """
    :param url: GET请求URL
    :param token: token
    :param params: 入参
    :return: JSON
    """
    try:
        response = requests.get(url, params=params, headers={'Content-Type': 'application/json; charset=UTF-8', 'x-amz-security-token': token})
        if response.status_code != 200:
            raise Exception("请求失败，status_code=" + str(response.status_code))
        print()
        print("*******************************************************************************")
        print("Request: " + url)
        print(params)
        print("-------------------------------------------------------------------------------")
        print("Response: ")
        result = json.loads(str(response.content, 'utf-8'))
        if len(response.content) < 200:
            print(result)
        else:
            print('success')
        '''
        if result['success'] is False:
            print(result)
        else:
            print("SUCCESS")
        '''
        print("*******************************************************************************")
        return result
    except Exception as e:
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
        print(e)
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")


def do_post(url, params, token=None):
    """
    :param url: POST请求URL
    :param params: DICT入参
    :param token: token
    :return: JSON
    """
    try:
        response = requests.post(url, data=json.dumps(params, ensure_ascii=False).encode('UTF-8'),
                                 headers={'Content-Type': 'application/json; charset=UTF-8', 'x-amz-security-token': token})
        if response.status_code != 200:
            raise Exception("请求失败，status_code="+str(response.status_code))
        print()
        print("*******************************************************************************")
        print("Request: " + url)
        print(json.dumps(params, ensure_ascii=False))
        print("-------------------------------------------------------------------------------")
        print("Response: ")
        result = json.loads(str(response.content, 'utf-8'))
        if len(response.content) < 200:
            print(result)
        else:
            print('success')
        print("*******************************************************************************")
        return result
    except Exception as e:
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
        print(e)
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")


def do_put(url, params, token=None):
    """
    :param url: PUT请求URL
    :param params: DICT入参
    :param token: token
    :return: JSON
    """
    try:
        response = requests.put(url, data=json.dumps(params, ensure_ascii=False).encode('UTF-8'), headers={'Content-Type': 'application/json; charset=UTF-8', 'token': token})
        if response.status_code != 200:
            raise Exception("请求失败，status_code="+str(response.status_code))
        print()
        print("*******************************************************************************")
        print("Request: " + url)
        print(json.dumps(params, ensure_ascii=False))
        print("-------------------------------------------------------------------------------")
        print("Response: ")
        result = json.loads(str(response.content, 'utf-8'))
        if len(response.content) < 200:
            print(result)
        else:
            print('success')
        print("*******************************************************************************")
        return result
    except Exception as e:
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
        print(e)
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")


def do_delete(url, token=None, params=None):
    """
    :param url: GET请求URL
    :param token: token
    :param params: 入参
    :return: JSON
    """
    try:
        response = requests.delete(url, params=params, headers={'Content-Type': 'application/json; charset=UTF-8', 'token': token})
        if response.status_code != 200:
            raise Exception("请求失败，status_code=" + str(response.status_code))
        print()
        print("*******************************************************************************")
        print("Request: " + url)
        print(params)
        print("-------------------------------------------------------------------------------")
        print("Response: ")
        result = json.loads(str(response.content, 'utf-8'))
        if len(response.content) < 200:
            print(result)
        else:
            print('success')
        '''
        if result['success'] is False:
            print(result)
        else:
            print("SUCCESS")
        '''
        print("*******************************************************************************")
        return result
    except Exception as e:
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
        print(e)
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
